package com.zhanggm.springcloud.jpa.repository;

import com.zhanggm.springcloud.jpa.entity.StuArea;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
@author Zhanggm
*/
public interface StuAreaRepository extends JpaRepository<StuArea, Integer> {

    /**
     * 根据pid查询地区列表
     * @param parentId
     * @return
     */
    @Query(value = "select * from 1804d.stu_area where pid=:parentId",nativeQuery = true)
    List<StuArea> selectAreaListByPid(@Param("parentId") Integer parentId);

    @Query(value = "select * from 1804d.stu_area where pid=:parentId",nativeQuery = true)
    Page<StuArea> selectAreaListByPid(@Param("parentId") Integer parentId, Pageable pageable);

    List<StuArea> findByPid(Integer pid);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query(value = "update 1804d.stu_area set name_en=:enName where id=:id",nativeQuery = true)
    int updateEnName(@Param("id") Integer id,@Param("enName") String enName);

    @Transactional
    @Modifying
    int deleteAllByCodeAndAndPid(String code,Integer pid);
}
